package connect_tx_sdk.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import connect_tx_sdk.config.QQConfig;
import connect_tx_sdk.utils.ConnectUtils;
import connect_tx_sdk.utils.HttpClientUtils;

/**
 * 
 * @创建作者：hiyoucai@126.com
 * @创建时间：2011-6-16 下午11:46:13
 * @文件描述：对应腾讯Step1：网站向Qzone发送request，请求临时token
 * @文件名称：connect.apps.qq.service.StepAToken.java
 */
public class TempToken {

	/**
	 * 
	 * 方法说明： API文档说明：<a href="http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91Qzone_OAuth%E8%AE%A4%E8%AF%81%E7%AE%80%E4%BB%8B" target="_blank">查看</a><br/>
	 * 创建日期：2011-6-17,下午02:59:09,hyc <br/>
	 */
	public String temp() throws Exception {
		String url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token";

		List<NameValuePair> parameters = new ArrayList<NameValuePair>();

		String oauth_timestamp = ConnectUtils.getOauthTimestamp();
		String oauth_nonce = ConnectUtils.getOauthNonce();

		parameters.add(new BasicNameValuePair("oauth_consumer_key", QQConfig.appid));
		parameters.add(new BasicNameValuePair("oauth_nonce", oauth_nonce));
		parameters.add(new BasicNameValuePair("oauth_signature_method", "HMAC-SHA1"));
		parameters.add(new BasicNameValuePair("oauth_timestamp", oauth_timestamp));
		parameters.add(new BasicNameValuePair("oauth_version", ConnectUtils.getOauthversion()));

		String oauth_signature = ConnectUtils.getOauthSignature("GET", url, parameters, "");
		parameters.add(new BasicNameValuePair("oauth_signature", oauth_signature));

		url += "?" + ConnectUtils.getSerialParameters(parameters, true);
		DefaultHttpClient httpclient = HttpClientUtils.getHttpClient();
		String html = HttpClientUtils.getHtml(httpclient, url, "UTF-8");

		return html;
	}

	public static void main(String[] args) throws Exception {
		if(true){			
			String html = new TempToken().temp();
			System.out.println(html);
		}
		
	}
}
